var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');

var path = {
  toolkit: [
    'toolkit/jquery.min.js',
    'toolkit/bootstrap.min.js',
    'toolkit/angular.min.js',
    'toolkit/angular-route.min.js'
  ],
  FermDeploy: [
    'FermDeploy/fd.base.js',
    'FermDeploy/fd.variable.js',
    'FermDeploy/fd.router.js',
    'FermDeploy/fd.other.js',
    'FermDeploy/fd.controller.js',
    'FermDeploy/fd.jquery-extend.js'
  ]
}

// 构建controller
gulp.task("buildFdCtrlJS", function () {
  return gulp.src('FermDeploy/controller/*.js')
    .pipe(concat('fd.controller.js'))
    .pipe(gulp.dest('./FermDeploy'));
});

// 构建js库
gulp.task("buildToolkitJS", function () {
  return gulp.src(path.toolkit)
    .pipe(concat('toolkit.js'))
    .pipe(gulp.dest('./build'));
});

// 构建系统js
gulp.task("buildFdMinJS", ["buildFdCtrlJS"], function () {
  // 压缩
  return gulp.src(path.FermDeploy)
    .pipe(concat('fd.main.min.js'))
    .pipe(uglify({
      mangle: false,
      compress: false
    }))
    .pipe(gulp.dest('./build'));
});

// 构建全部
gulp.task("buildMainMinJS", ["buildToolkitJS", "buildFdMinJS"], function () {
  // 压缩
  gulp.src(["build/toolkit.js", "build/fd.main.min.js"])
    .pipe(concat('main.min.js'))
    .pipe(gulp.dest('./build'));
});

// 构建任务
gulp.task("buildJS", ["buildMainMinJS"], function () {
  console.log("BuildJS done!");
});

// 实时构建
gulp.task("watchJS", function () {
  gulp.watch([
    'FermDeploy/controller/*.js',
    'FermDeploy/fd.base.js',
    'FermDeploy/fd.variable.js',
    'FermDeploy/fd.router.js',
    'FermDeploy/fd.other.js',
    'FermDeploy/fd.jquery-extend.js'
  ], ["buildJS"]);
});